/*************************
 * Redéfinition de fonctions ou de plugin
 */
// Redefinition de Array.indexOf pour ie, safari et opera
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

// Redefinition du plugin dropdown de bootstrap v2.0.0
$.extend($.fn.dropdown.Constructor.prototype, {
	// Ajout de align dans la fonction toggle
	toggle: function ( e ) {
    var $this = $(this)
      , selector = $this.attr('data-target')
      , align = $this.attr('data-align')
      , $parent
      , isActive
      
    if (!selector) {
      selector = $this.attr('href')
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
    }
    
    if (!align)	align = 'left'

    $parent = $selector = $(selector)
    if($parent.length == 0){
    	$parent = $this.parent() 
    	$selector = $this.next('ul')
    }

    isActive = $parent.hasClass('open')

    $('[data-toggle="dropdown"]').parent().removeClass('open')
    if(!isActive) $parent.toggleClass('open')
    
    if (align == 'right') $selector.removeClass('dropdown-menu-left').addClass('dropdown-menu-right');
    else $selector.removeClass('dropdown-menu-right').addClass('dropdown-menu-left');
    
    return false
  }
}); 

/*************************
 * Fonctions communes lancées au chargement de la page
 */
$(document).ready(function(){
	$(".alert .close").click(function(){
		$(this).parents(".alert").hide();
		return false;
	});
	
	$(".modal .close").click(function(){
		$(this).parents(".modal").modal('hide');
		return false;
	});
});

/*************************
 * Fonctions communes
 */
function effacerErreurs(id){
	$("#"+id+" .error").each(function(){
		$(this).removeClass('error');
	});
	
	$("#"+id+" .txtErreur").each(function(){
		$(this).remove();
	});
	
	$('.nav-tabs img', "#"+id).hide();
}

/*************************
 * Plugin myLoader (LoaderAlert)
 */
var myLoader = myLoader || {};

myLoader.LoaderAlert = function(o){
    this._options = {
        src: '',
        template: '<div id="mainTopLoader"><div id="contentLoader" class="alert"><a class="close right" style="right:0;" href="#">×</a><img src="" alt="Chargement ..." /> <span id="textLoader">Patientez ...</span></div></div>',
              
        onFinish: function(){},
        onStart: function(){}                      
    };
    
    $.extend(this._options, o);
    this._locked = new Array();
    this._loader = this._createLoader();
};

myLoader.LoaderAlert.prototype = {
	// Fonctions internes
	_createLoader: function(){
		var loader = null;
		if($('#mainTopLoader').size() != 0)
			$('#mainTopLoader').remove();
		$('body').append(this._options.template);
		loader = $('#mainTopLoader');
		loader.children('div').children('img').attr({'src' : this._options.src });
		return loader;
	},
	
	// Fonctions public
	setContent: function(html){
		$('#contentLoader', this._loader).html(html);
	},	
	setText: function(html){
		if($('#textLoader', this._loader).size() != 0)
			$('#textLoader', this._loader).html(html);
	},
    show: function(){
		if(parseInt(this._loader.css('top')) <= 0){
			this._loader = this._createLoader();
			$('#contentLoader', this._loader).removeClass('alert-success');
			$('#contentLoader', this._loader).removeClass('alert-error');
			$('#contentLoader', this._loader).addClass('alert-warning');
			this._loader.animate({
			    opacity: 1,
			    top: '+=70'
			}, 300, function() {});
		}
		return true;
    },
    hide: function(erreur){
    	var loader = this._loader;
    	if($('#mainTopLoader').size() != 0 && parseInt(this._loader.css('top')) > 0){
    		if(typeof erreur == 'undefined'){
	    		this.setContent('Terminé !');
				$('#contentLoader', loader).removeClass('alert-warning');
				$('#contentLoader', loader).addClass('alert-success');
	    		setTimeout(function(){
	    			loader.animate({
		    			opacity: 0,
					    top: '-=70'
					}, 300, function() {});
	    		},1000);
    		} else {
    			this.setContent(erreur);
				$('#contentLoader', loader).removeClass('alert-warning');
				$('#contentLoader', loader).addClass('alert-error');
	    		setTimeout(function(){
	    			loader.animate({
		    			opacity: 0,
					    top: '-=70'
					}, 300, function() {});
	    		},10000);
    		}
    	}
    },
    lock: function(signName){
    	if(this._locked.indexOf(signName) >= 0)
    		return true;
    	
    	this._locked.push(signName);
    	return false;
    },
    delock: function(signName){
    	if(this._locked.indexOf(signName) >= 0)
    		this._locked.splice(this._locked.indexOf(signName),1);
    }   
};

function removeValFromString(string,val,separator){
	var array = string.split(separator);
	if(array.indexOf(val) >= 0) array.splice(array.indexOf(val),1);
	return array.join(separator);
}

function addValFromString(string,val,separator){
	var array = string.split(separator);
	if(array.indexOf("") >= 0) array.splice(array.indexOf(""),1);
	if(array.indexOf(val) < 0) array.push(val);
	return array.join(separator);
}